Registry.loadFile("assets/manifest.js");
var Kankan = {
  browser: null,
  plugins: [],
  isInitialized: false,
  init: function() {
    if(Kankan.isInitialized) return;
    Kankan.initPlugins();
    if(typeof chrome !== "undefined") {
      Kankan.browser = chrome;
    }
    Kankan.browser = null;

  },
  isKanKan: function() {
    return true;
  },
  // 搜索
  searchVideo: function(query) {
    Registry.loadFile("assets/plugins/plugins-interface.js");
    Registry.loadFile("assets/plugins/iyf/iyf-search.js");
    console.log(Registry.classes);
    let cn = "IyfSearch";
    console.log(new cn());
    return;


    Kankan.init();
    // console.log(Kankan.plugins);
    for(const p of Kankan.plugins) {
      if(typeof p.class_name === "string") {
        let obj = Registry.getClass(p.class_name);
        console.log(obj);
      }
    }
  },
  // 加载插件
  initPlugins() {
    let m = new Manifest("assets/plugins/manifest.json");
    let pluginList = m.loadPlugins();
    console.log(pluginList);
    Kankan.plugins.push(...pluginList);
    if(m.manifests) {
      for(let file of m.manifests) {
        m = new Manifest(file);
        let pluginList = m.loadPlugins();
        Kankan.plugins.push(...pluginList);
      }
    }

    /*let manifest = Kankan.loadManifest(file);
    console.log(manifest);
    new Plugin(manifest);
    if(manifest["manifests"]) {
      manifest["manifests"].forEach(file => {
        manifest = Kankan.loadManifest(file);
        Kankan.plugins.push(new Plugin(manifest))
      });
    }*/
  },
  // 加载manifest文件
  loadManifest(file) {
    return JSON.parse(Registry.getRaw(file));
  },
  // 渲染搜索结果
  renderSearchList(newResults, siteInFetching) {
    if(siteInFetching === 0) {
      document.getElementById('status').innerText = '';
    } else {
      document.getElementById('status').innerText = `(${siteInFetching} sites in searching...)`;
    }

    if(newResults == null) return;
    let content = '';
    newResults.forEach(item => {
      content += `
      <a class="result-item" href="${item.link}">
        <div class="result-image-wrapper">
          <img src="${item.image}" alt="${item.title}" class="result-image" onerror="this.style.display='none';">
          <div class="site-info">
            <img src="${item.site_icon}" alt="${item.site_name}" class="site-icon">
            <span class="site-name">${item.site_name}</span>
          </div>
        </div>
        <div class="result-info">
          <p class="result-title">${item.title}</p>
          <p class="result-description">${item.description}</p>
        </div>
      </a>
    `;
    });
    document.getElementById('results').innerHTML += content;
  },
}

// Kankan.init();